﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;

namespace FERJ.SiteInstitucional.Repositorio.Log
{
    public class GeradorLog
    {
        public string caminho
        {
            get
            {
                if (ConfigurationManager.AppSettings["caminhoLogClube"] != null)
                {
                    return ConfigurationManager.AppSettings["caminhoLogClube"].ToString();
                }
                else if (ConfigurationManager.AppSettings["caminhoLogAdmin"] != null)
                {
                    return ConfigurationManager.AppSettings["caminhoLogAdmin"].ToString();
                }
                return ConfigurationManager.AppSettings["caminhoLog"].ToString();
            }
        }


        public void Error(Exception ex)
        {
            try
            {
                //string basepath = AppDomain.CurrentDomain.BaseDirectory;
                //string file = string.Format("{0}{1}\\{2}.txt", basepath, caminho, DateTime.Now.ToString("dd-MM-yyyy"));
                string file = string.Format("{0}\\{1}.txt", caminho, DateTime.Now.ToString("dd-MM-yyyy"));

                using (StreamWriter sw = new StreamWriter(file, File.Exists(file)))
                {
                    if (ex.InnerException == null)
                    {
                        sw.WriteLine("{0} - Message : {1} \n Stack Trace : {2}", DateTime.Now.ToString(), ex.Message, ex.StackTrace);
                    }
                    else
                    {
                        sw.WriteLine("{0} - Message : {1} \n Stack Trace : {2} \n\n InnerException Message: {3} \n InnerException StackTrace : {4}", DateTime.Now.ToString(), ex.Message, ex.StackTrace, ex.InnerException.Message, ex.InnerException.StackTrace);
                    }
                }
            }
            catch (UnauthorizedAccessException e)
            {
                throw e;
            }
        }
    }
}
